package socketmvc.core.packets.biz;

import socketmvc.core.lambda.WrapExceptionSupplier;

import java.io.*;

public class SockFile implements Serializable, Comparable<SockFile> {

    private String fileName;
    private InputStream in;
    private final int size;
    /*isPart标识是否是大文件的一部分*/
    private boolean part = Boolean.FALSE;
    /*当isPart标识值等于true的时候，该字段有值，totalLength标识大文件共多少字节*/
    private Integer totalLength;
    /*当isPart标识值等于true的时候，该字段有值，startIndex标识当前部分在大文件中的字节数范围起始索引*/
    private Integer startIndex;
    /*当isPart标识值等于true的时候，该字段有值，endIndex标识当前部分在大文件中的字节数范围结束索引*/
    private Integer endIndex;


    public SockFile(byte[] bytes, String fileName) {
        this.in = new ByteArrayInputStream(bytes);
        this.fileName = fileName;
        this.size = bytes.length;
    }

    public SockFile(File file){
        this.fileName = file.getName();
        this.in = WrapExceptionSupplier.handleAsCheckedException(()->new FileInputStream(file));
        this.size = (int) file.length();
    }

    public SockFile(InputStream inputStream, String fileName) {
        this.in = inputStream;
        this.fileName = fileName;
        this.size = WrapExceptionSupplier.handleAsCheckedException(inputStream::available);
    }


    public void transferTo(File to) {
        FileOutputStream outputStream = WrapExceptionSupplier.handleAsCheckedException(()->new FileOutputStream(to));
        transferTo(outputStream);
    }

    public void transferTo(OutputStream toStream){
        InputStream inputStream = getInputStream();
        WrapExceptionSupplier.handleAsCheckedException(()->{
            try(inputStream;toStream){
                inputStream.transferTo(toStream);
            }
            return 1;
        });
    }


    public InputStream getInputStream(){
        return in;
    }

    public byte[] getBytes(){
        return WrapExceptionSupplier.handleAsCheckedException(()-> in.readAllBytes());
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public int getSize() {
        return size;
    }

    public boolean isPart() {
        return part;
    }

    public void setPart(boolean part) {
        this.part = part;
    }

    public void part(int totalLength,int start, int end) {
        this.part = Boolean.TRUE;
        this.totalLength = totalLength;
        this.startIndex = start;
        this.endIndex = end;
    }

    public Integer getTotalLength() {
        return totalLength;
    }

    public void setTotalLength(Integer totalLength) {
        this.totalLength = totalLength;
    }

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    public Integer getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(Integer endIndex) {
        this.endIndex = endIndex;
    }

    /**
     * 实现compareTo是为了可以使用jdk提供的排序功能
     * 默认使用文件体积进行比较
     * @param o 另一个文件
     * @return 0代表相等，1代表大于，-1代表小于
     */
    @Override
    public int compareTo(SockFile o) {
        return Integer.compare(this.getSize(),o.getSize());
    }

}
